//
//  TitleTextFieldCell.m
//  MoneyIndividual
//
//  Created by Tai Le on 12/30/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "TitleTextFieldCell.h"
#import "CalculatorView.h"

@implementation TitleTextFieldCell

#pragma mark - Init
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        
        _textFieldMode = TextFieldInputModeKeyboard;
        [self initializeTextField];
        [self initializeToolbar];
    }
    
    return self;
}

- (void)initializeTextField
{
    InsetTextField *textField = [[InsetTextField alloc] initWithInsetLeft:0 insetRight:30];
    [textField setFont:FONT(FONT_SIZE_NORMAL)];
    [textField setBackgroundColor:[UIColor clearColor]];
    textField.delegate = self;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.returnKeyType = UIReturnKeyDone;
    textField.textAlignment = NSTextAlignmentRight;
    [_subView addSubview:textField];
    
    _textField = textField;
}

- (void)initializeToolbar
{
    _toolbar = [[InputToolbar alloc] init];
    _toolbar.toolbarDelegate = self;
    _textField.inputAccessoryView = _toolbar;
}

- (void)loadData:(NSManagedObject *)managedObject
{
    if (managedObject == nil || [managedObject isKindOfClass:[NSManagedObject class]] == NO)
    {
        return;
    }
    
    id value = [managedObject valueForKey:_key];
    
    self.selectedValue = value;
}

- (void)setSelectedValue:(id)selectedValue
{
    _selectedValue = selectedValue;
    
    if ([selectedValue isKindOfClass:[NSString class]])
    {
        _textField.text = selectedValue;
    }
    else if ([selectedValue isKindOfClass:[NSNumber class]])
    {
        _textField.text = [NSString stringWithInt:[selectedValue integerValue]];
    }
    else if ([selectedValue isKindOfClass:[NSDate class]])
    {
        _textField.text = [selectedValue stringyyyymmdd];
    }    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    if (selected)
    {
        [_textField becomeFirstResponder];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _textField.frame = RECT(self.titleLabel.right + PADDING, 0, _subView.width - self.titleLabel.right - PADDING, _subView.height);
    
}

#pragma mark - TextField delegate

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if ([_delegate respondsToSelector:@selector(textFieldCellDidBeginEditting:)])
    {
        [_delegate textFieldCellDidBeginEditting:self];
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([_delegate respondsToSelector:@selector(textFieldCellDidEndEditting:)])
    {
        [_delegate textFieldCellDidEndEditting:self];
    }
    
    if (_ignoreRaiseDidSelectValue)
    {
        return;
    }
    
    if ([_delegate respondsToSelector:@selector(textFieldCellDidSelectValue:)])
    {
        [_delegate textFieldCellDidSelectValue:self];
    }
}

- (void)inputToolbarDidPressCloseButton:(InputToolbar *)toolbar
{
    if (self.textFieldMode == TextFieldInputModeTableView)
    {
        _ignoreRaiseDidSelectValue = YES;
        [_textField resignFirstResponder];
        _ignoreRaiseDidSelectValue = NO;
    }
    else
    {
        [_textField resignFirstResponder];
    }
}

- (void)inputToolbarDidPressRightButton:(InputToolbar *)toolbar
{
    [_textField resignFirstResponder];
}
@end
